﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class SelectPlaneForm : System.Windows.Forms.Form
	{
		public SelectPlaneForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public SelectPlaneForm(int layType, string offSet, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.layType = layType;
			this.hytbPlaneOffset.Text = offSet;
			this.doc = doc;
		}

		public SelectPlaneForm(string offSet, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.layType = 0;
			this.hytbPlaneOffset.Text = offSet;
			this.doc = doc;
		}

		private void SelectPlaneForm_Load(object sender, EventArgs e)
		{
			if (this.doc != null)
			{
				this.FilterLevelPlane();
				if (this.lsbPlane.Items.Count > 0)
				{
					this.lsbPlane.SelectedItem = this.lsbPlane.Items[0];
				}
			}
			if (this.layType != 0)
			{
				this.rdoVertical.Enabled = false;
			}
			this.hytbPlaneName.Enabled = false;
			this.hytbPlaneOffset.Enabled = false;
			if (RevitVersion.Version != "2012")
			{
				this.rdoVertical.Checked = true;
				this.rdoLevel.Enabled = false;
				this.rdoCustom.Enabled = false;
				this.rdoVertical.Enabled = true;
			}
		}

		private void FilterLevelPlane()
		{
			if (2 != (int)this.doc.ActiveView.ViewType && 1 != (int)this.doc.ActiveView.ViewType)
			{
				return;
			}
			ElementId id = this.doc.ActiveView.GenLevel.Id;
			IList<Element> elementByCategory = YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_CLines);
			double num = this.activeLevelHeight;
			if (null != id)
			{
				this.activeLevelHeight = (this.doc.GetElementById(id) as Level).Elevation;
				num = this.activeLevelHeight;
			}
			IEnumerable<Element> elementByCategory2 = YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_Levels);
			double elevation = this.activeLevelHeight;
			double num2 = 100.0;
			foreach (Element element in elementByCategory2)
			{
				if (element is Level)
				{
					elevation = (element as Level).Elevation;
					if (elevation - this.activeLevelHeight > 0.0001 && elevation - this.activeLevelHeight < num2)
					{
						num2 = elevation - this.activeLevelHeight;
						num = elevation;
					}
				}
			}
			this.lsbPlane.ValueMember = "PlaneId";
			this.lsbPlane.DisplayMember = "PlaneName";
			ListItemPlane listItemPlane = new ListItemPlane(0, "楼层平面", 0);
			listItemPlane.OffSet = this.activeLevelHeight;
			this.lsbPlane.Items.Add(listItemPlane);
			if (Math.Abs(num - this.activeLevelHeight) > 0.0001)
			{
				ListItemPlane listItemPlane2 = new ListItemPlane(0, "天花板平面", 1);
				listItemPlane2.OffSet = num;
				this.lsbPlane.Items.Add(listItemPlane2);
			}
			foreach (Element element2 in elementByCategory)
			{
				if (element2 is ReferencePlane && ((element2 as ReferencePlane).Normal.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || (element2 as ReferencePlane).Normal.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0))))
				{
					double z = (element2 as ReferencePlane).GetPlane().Origin.Z;
					if (z > this.activeLevelHeight - 0.0001 && z < num + 0.0001)
					{
						ListItemPlane listItemPlane3 = new ListItemPlane(element2.Id.IntegerValue, element2.Name.Trim(), 2);
						listItemPlane3.OffSet = z - this.activeLevelHeight;
						this.lsbPlane.Items.Add(listItemPlane3);
					}
					else if (Math.Abs(this.activeLevelHeight - num) < 0.0001 && z > this.activeLevelHeight - 0.0001)
					{
						ListItemPlane listItemPlane4 = new ListItemPlane(element2.Id.IntegerValue, element2.Name.Trim(), 2);
						listItemPlane4.OffSet = z - this.activeLevelHeight;
						this.lsbPlane.Items.Add(listItemPlane4);
					}
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (2 != (int)this.doc.ActiveView.ViewType && 1 != (int)this.doc.ActiveView.ViewType)
			{
				MessageBox.Show("请在平面视图中进行布置", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.planeParam = new ListItemPlane();
			if (this.rdoLevel.Checked)
			{
				this.planeParam = (this.lsbPlane.SelectedItem as ListItemPlane);
			}
			else if (this.rdoVertical.Checked)
			{
				this.planeParam.PlaneType = 4;
			}
			else
			{
				foreach (Element element in ((IEnumerable<Element>)YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_CLines)))
				{
					if (element is ReferencePlane && (element as ReferencePlane).Name == this.hytbPlaneName.Text.Trim())
					{
						MessageBox.Show("平面名称重复", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						this.hytbPlaneName.Focus();
						return;
					}
				}
				this.planeParam.PlaneName = this.hytbPlaneName.Text.Trim();
				this.planeParam.PlaneType = 3;
				this.planeParam.OffSet = this.activeLevelHeight + UnitConvert.CovertToAPI(Convert.ToDouble(this.hytbPlaneOffset.Text) * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2);
			}
			base.DialogResult = DialogResult.OK;
		}

		private void rdoLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLevel.Checked)
			{
				this.lsbPlane.Enabled = true;
				return;
			}
			this.lsbPlane.Enabled = false;
		}

		private void rdoCustom_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoCustom.Checked)
			{
				this.hytbPlaneName.Enabled = true;
				this.hytbPlaneOffset.Enabled = true;
				return;
			}
			this.hytbPlaneName.Enabled = false;
			this.hytbPlaneOffset.Enabled = false;
		}

		private void lsbPlane_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lsbPlane.SelectedItem != null)
			{
				this.tbLevelOffset.Text = StringTools.CutOffEfficacious((NumbericTools.feetTomm((this.lsbPlane.SelectedItem as ListItemPlane).OffSet) / 1000.0).ToString(), 2);
			}
		}

		private int layType;

		private Document doc;

		public ListItemPlane planeParam;

		private double activeLevelHeight;
	}
}
